package com.nlx.notes.module.service.impl.gold;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nlx.notes.module.bean.vo.flow.GoldSaleSumOverviewVO;
import com.nlx.notes.module.core.consts.GlobalConst;
import com.nlx.notes.module.entity.GoldPurchaseEntity;
import com.nlx.notes.module.entity.GoldSaleOverviewEntity;
import com.nlx.notes.module.mapper.GoldPurchaseServiceMapper;
import com.nlx.notes.module.mapper.GoldSaleOverviewMapper;
import com.nlx.notes.module.service.gold.GoldSaleOverviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Slf4j
@Service
public class GoldSaleOverviewServiceImpl extends ServiceImpl<GoldSaleOverviewMapper, GoldSaleOverviewEntity> implements GoldSaleOverviewService {

    @Override
    public GoldSaleOverviewEntity findByUserId(Long userId) {
        if(null == userId){
            return null;
        }
        return this.getBaseMapper().selectOne(Wrappers.lambdaQuery(GoldSaleOverviewEntity.class)
                .eq(GoldSaleOverviewEntity::getUserId,userId)
                .eq(GoldSaleOverviewEntity::getDelFlag, GlobalConst.DEL_FLAG_NORMAL),false);
    }

    @Override
    public GoldSaleOverviewEntity findIfNotExistCreateByUserId(Long userId) {
        if(null == userId){
            return null;
        }
        GoldSaleOverviewEntity saleOverview = this.findByUserId(userId);
        if(null == saleOverview){
            if(this.createGoldSaleOverViewByUserId(userId)){
                return this.findByUserId(userId);
            }else{
                return null;
            }
        }
        return saleOverview;

    }

    @Override
    public void updateGoldSale(GoldSaleSumOverviewVO goldSaleSumOverviewVO) {
        GoldSaleOverviewEntity overviewEntity = this.findIfNotExistCreateByUserId(goldSaleSumOverviewVO.getUserId());
        overviewEntity.setWeight(goldSaleSumOverviewVO.getWeight());
        overviewEntity.setBuyTotalPrice(goldSaleSumOverviewVO.getBuyTotalPrice());
        overviewEntity.setSaleTotalPrice(goldSaleSumOverviewVO.getSaleTotalPrice());
        overviewEntity.setBuyUnitPrice(goldSaleSumOverviewVO.getBuyUnitPrice());
        overviewEntity.setSaleUnitPrice(goldSaleSumOverviewVO.getSaleUnitPrice());
        overviewEntity.setTotalProfitLoss(goldSaleSumOverviewVO.getTotalProfitLoss());
        overviewEntity.setProfit(goldSaleSumOverviewVO.getProfit());
        overviewEntity.setUpdatedTime(LocalDateTime.now());
        this.updateById(overviewEntity);
    }

    private boolean createGoldSaleOverViewByUserId(Long userId) {
        BigDecimal zero = BigDecimal.ZERO;
        GoldSaleOverviewEntity goldSaleOverviewEntity = new GoldSaleOverviewEntity();
        goldSaleOverviewEntity.setWeight(zero);
        goldSaleOverviewEntity.setBuyTotalPrice(zero);
        goldSaleOverviewEntity.setSaleTotalPrice(zero);
        goldSaleOverviewEntity.setBuyUnitPrice(zero);
        goldSaleOverviewEntity.setSaleUnitPrice(zero);
        goldSaleOverviewEntity.setFee(zero);
        goldSaleOverviewEntity.setTotalProfitLoss(zero);
        goldSaleOverviewEntity.setUserId(userId);
        goldSaleOverviewEntity.setRemarks(null);
        goldSaleOverviewEntity.setCreatedTime(LocalDateTime.now());
        goldSaleOverviewEntity.setDelFlag(GlobalConst.DEL_FLAG_NORMAL);
        return this.save(goldSaleOverviewEntity);
    }
}
